<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/**
 *  Advertising Model Class  
 *  
 * @package XGO CMS v2.0 
 * @subpackage News  
 * @author Manh Nguyen - 06/08/2012 
 * @link http://sunsoft.vn
 */
class Menu_model extends DataMapper {

    var $model = 'menu_model';
    var $table = 'menu';

    // --------------------------------------------------------------------
    // Construct
    // --------------------------------------------------------------------
    public function __construct() {
        parent::__construct();
        $this->validation = array(
            'new_page' => array(
                'values' => array(
                    '0' => $this->lang->line('no'),
                    '1' => $this->lang->line('yes')
                )
            ),
            'position' => array(
                'values' => array(
                    'top'       => $this->lang->line('top'),
                    'right'     => $this->lang->line('right'),
                    'bottom'    => $this->lang->line('bottom'),
                    'left'      => $this->lang->line('left')
                )
            ),
            'active' => array(
                'values' => array(
                    '0'     => $this->lang->line('no'),
                    '1'     => $this->lang->line('yes')
                )
            ),
            'type' => array(
                'values' => array(
                    'homepage'  => $this->lang->line('homepage'),
                    'admin'     => 'Admin'
                )
            )
    );
    }

    // --------------------------------------------------------------------
    // Relationships
    // --------------------------------------------------------------------
    var $has_one = array();
    var $has_many = array();
    // --------------------------------------------------------------------
    // Validation
    // --------------------------------------------------------------------	
    var $validation = array();
    // --------------------------------------------------------------------
    /**
     * Default order in query select 
     */
    var $default_order_by = array('created');

    // --------------------------------------------------------------------
    /**
     * string value of object
     * 
     * @access private
     * @param null
     * @return string 
     */
    function __toString() {
        return empty($this->title) ? '---empty---' : $this->title;
    }
    
    function get_parent_by_id($child_id='')
    {
        $this->select('title, code, id');
        $where="`id`= (select `menu`.`parent_id` from `menu` where `menu`.`id`=$child_id)";
        $this->strwhere($where);
        $this->get();
        return $this;
    }
    
    function get_list_by_parent_id($parent_id = '')
    {
        $this->select('title, code, link, id');
        $this->where('parent_id',$parent_id);
        $this->where('active',1);
        $this->order_by('order','asc');
        return $this->get();
    }
    
    function get_list($position = NULL, $type = NULL, $get_parent_list = TRUE) {       
    	$this->select('id, title, new_page, link');
    	$this->where('position', $position);
    	$this->where('type', $type);
    	$this->where('active', 1);
    	if($get_parent_list) {
    		$this->where('parent_id', 0);
    	}      
    	return $this->get();
    }
     function get_list_bottom($position = NULL, $type = NULL) {       
         $menu = new Menu_model();
    	$menu->select('id, title, new_page, link');
    	$menu->where('position', $position);
    	$menu->where('type', $type);
    	$menu->where('active', 1);               
    	return $menu->get();
    }
    
    public function get_sub_menu_list($parent_id, $level = 1) {
    	$menu = new Menu_model();
    	$menu->select('id, title, new_page, link');
    	$menu->where('active', 1);
        $menu->where('parent_id', $parent_id);
    	return $menu->get();    	
    }
    
    public function count_sub_menu($parent_id, $level = 1) {
    	$menu = new Menu_model();
    	$menu->select('id, title, new_page, link');
    	$menu->where('active', 1);
    	$menu->where('parent_id', $parent_id);
    	return $menu->get();
    }

    // --------------------------------------------------------------------
    /**
     * array active of object
     * 
     * @access public
     * @param bool
     * @return array
     */
    function active_list($zero = FALSE) {
        $active_list = $this->validation['active']['values'];
        if ($zero) {
            $active_list = array_merge(array('' => '--'.$this->lang->line('all').'--'), $active_list);
        }
        return $active_list;
    }

    // --------------------------------------------------------------------
    /**
     * array require of object
     * 
     * @access public
     * @param bool
     * @return array
     */
    function position_list($zero = FALSE) {
        $position_list = $this->validation['position']['values'];
        if ($zero) {
            
        }
        return $position_list;
    }

    /**
     * array new_page of object
     * 
     * @access public
     * @param bool
     * @return array
     */
    function new_page_list($zero = FALSE) {
        $new_page_list = $this->validation['new_page']['values'];
        if ($zero) {
            $new_page_list = array_merge(array('' => '--'.$this->lang->line('all').'--'), $new_page_list);
        }
        return $new_page_list;
    }

    /**
     * array type of object
     *
     * @access public
     * @param bool
     * @return array
     */
    function type_list($zero = FALSE, $type = '') {
        if (empty($type)) {
            $type_list = $this->validation['type']['values'];
        } else {
            $type_list = array($type => $type);
        }
        if ($zero) {
            $type_list = array_merge(array('' => '--'.$this->lang->line('all').'--'), $type_list);
        }
        return $type_list;
    }

    

    /**
     * string value of new_page
     *
     * @access public
     * @param obj
     * @return bool
     */
    function get_new_page() {
        return $this->validation['new_page']['values'][$this->new_page];
    }

    /**
     * string value of active
     *
     * @access public
     * @param obj
     * @return bool
     */
    function get_active() {
        return $this->validation['active']['values'][$this->active];
    }

    /**
     * string value of type
     *
     * @access public
     * @param object
     * @return bool
     */
    function get_type() {
        return $this->validation['type']['values'][$this->type];
    }

    /**
     * string value of require
     *
     * @access public
     * @param obj
     * @return bool
     */
    function get_position() {
        return $this->validation['position']['values'][$this->position];
    }

    /**
     * array metadata for generate jquery grid. 
     * 
     * @access public
     * @param null
     * @return array
     */
    function jqgrid_metadata() {
        //edittype: 'text', 'textarea', 'select', 'checkbox', 'password', 'button', 'image', 'file', 'custom' 
        $menu_list = array(
            '' => '--'.$this->lang->line('all').'--',
            '0' => '--'.$this->lang->line('root_menu').'--'
        );

        $this->get_list_title($menu_list, FALSE, 2);
        return array(
            'names' => array(
                'id',
                $this->localize_label('title'),
                $this->lang->line('link'),
                $this->localize_label('new_page'),
                $this->localize_label('type'),
                $this->localize_label('index'),
                $this->lang->line('active'),
                $this->localize_label('parent_id'),
                $this->localize_label('require')
            ),
            'model' => array(
                array('name' => 'id', 'hidden' => TRUE, 'key' => TRUE,
                    'editrules' => array('edithidden' => TRUE)
                ),
                array('name' => 'title', 'width' => '300',
                    'searchoptions' => array(
                        'sopt' => array('cn', 'nc', 'eq', 'bw', 'bn', 'ew', 'en')
                    )
                ),
                array('name' => 'link',
                    'searchoptions' => array(
                        'sopt' => array('cn', 'nc', 'eq', 'bw', 'bn', 'ew', 'en')
                    )
                ),
                array('name' => 'new_page',
                    'searchoptions' => array(
                        'sopt' => array('eq'),
                        'value' => $this->new_page_list(TRUE)
                    ),
                    'stype' => 'select'
                ),
                array('name' => 'type',
                    'searchoptions' => array(
                        'sopt' => array('eq'),
                        'value' => $this->type_list(TRUE)
                    ),
                    'stype' => 'select'
                ),
                array('name' => 'order',
                    'searchoptions' => array(
                        'sopt' => array('eq', 'gt', 'ge', 'lt', 'le')
                    )
                ),
                array('name' => 'active',
                    'searchoptions' => array(
                        'sopt' => array('eq'),
                        'value' => $this->active_list(TRUE)
                    ),
                    'stype' => 'select'
                ),
                array('name' => 'parent_id',
                    'searchoptions' => array(
                        'sopt' => array('eq'),
                        'value' => $menu_list
                    ),
                    'stype' => 'select'
                ),
                array('name' => 'position',
                    'searchoptions' => array(
                        'sopt' => array('eq'),
                        'value' => $this->position_list(TRUE)
                    ),
                    'stype' => 'select'
                )
            )
        );
    }

    // --------------------------------------------------------------------
    /**
     * rewite delete function to check exists sub object before delete. 
     * 
     * @access public
     * @param obj, string
     * @return -1 or bool
     */
    function delete($object = '', $related_field = '') {
        if (empty($object) && !is_array($object) && $this->id > 0) {
            $menu_list = $this->get_one_sub(array('select' => 'id'));
            if ($menu_list->result_count() > 0) {
                return -1;
            }
        }
        return parent::delete($object, $related_field);
    }

    // --------------------------------------------------------------------
    /**
     * level of object in tree of object.
     * 
     * @access public
     * @param null
     * @return int
     */
    function level() {
        if ($this->id > 0) {
            if ($this->parent_id == 0)
                return 1;
            $menu = new Menu_model();
            $menu->select('id, parent_id');
            $menu->where('id', $this->parent_id);
            $menu->get(1);
            return 1 + $menu->level();
        }
        return 0;
    }

    // --------------------------------------------------------------------
    /**
     * list of object are one sub level of this object.
     * 
     * @access public
     * @param array
     * @return objs
     */
    function get_one_sub($para = array()) {
        if ($this->id > 0) {
            $menu = new Menu_model();
            if (!empty($para['select']))
                $menu->select($para['select']);
            $menu->where('parent_id', $this->id);
            //echo $menu->get_sql();exit;
            if (!empty($para['notin_id']))
                $menu->where_not_in('id', $para['notin_id']);
            if (!empty($para['type'])) {
                if (is_array($para['type']))
                    $menu->where_in('type', $para['type']);
                else
                    $menu->where('type', $para['type']);
            }
            if (isset($para['user'])) {
                $menu->group_start();
                $menu->where('require', 0); // none
                if ($para['user'] != FALSE && $para['user']->id > 0) {
                    $menu->or_where('require >=', $para['user']->group->id); // login and require group
                } else {
                    $menu->or_where('require', -1); //logout
                }
                $menu->group_end();
            }
            return $menu->get();
        }
        return new Menu_model();
    }

    /**
     * get_one_sub_menu
     * 
     * @param type 
     * @return 
     */
    function get_one_sub_menu($para = array()) {
        if ($this->id > 0) {
            $menu = new Menu_model();
            if (!empty($para['select']))
                $menu->select($para['select']);
            $menu->where('parent_id', $this->id);
            if (!empty($para['notin_id']))
                $menu->where_not_in('id', $para['notin_id']);
            if (!empty($para['type'])) {
                if (is_array($para['type']))
                    $menu->where_in('type', $para['type']);
                else
                    $menu->where('type', $para['type']);
            }
            if (isset($para['user'])) {
                $menu->group_start();
                $menu->where('require', 1); // none
                if ($para['user'] != FALSE && $para['user']->id > 0) {
                    $menu->or_where('require >=', $para['user']->group->id); // login and require group
                } else {
                    $menu->or_where('require', -1); //logout
                }
                $menu->group_end();
            }
            return $menu->get();
        }
        return new Menu_model();
    }

    /**
     * list of object are child of this object (include level).
     * 
     * @access public
     * @param array get, int, array para
     * @return void
     */
    function get_all_sub(&$list, $level = 1, $para = array()) {
        if (!isset($para['notin_id']))
            $para['notin_id'] = array();
        if ($this->id > 0 && !in_array($this->id, $para['notin_id'])) {
            foreach ($this->get_one_sub($para) as $o) {
                $o->level = $level;
                $list[] = $o;
                $o->get_all_sub($list, $level + 1, $para);
            }
        }
    }

    /**
     * list of id and title of object, user for create options.
     * 
     * @access public
     * @param array get, array para
     * @return void
     */
    function get_list_title(&$listname, $para = array()) {
        $notin_id = array();
        if (!isset($para['check']))
            $check = FALSE;
        else
            $check = $para['check'];
        if ($check && $this->id > 0) {
            $notin_id[] = $this->id;
            $list = array();
            $this->get_all_sub($list, 1, array('select' => 'id'));
            foreach ($list as $menu) {
                $notin_id[] = $menu->id;
            }
        }
        $select = 'id, title';
        $menu = new Menu_model();
        $menu->select($select);
        $menu->where('parent_id', 0);
        if (!empty($para['type']))
            $menu->where('type', $para['type']);
        if (!empty($para['language']))
            $menu->where_related('language', 'id', $para['language']);
        // Lay tham so gioi han level cua danh muc
        if (!isset($para['maxlevel']))
            $maxlevel = 0;
        else
            $maxlevel = $para['maxlevel'];
        foreach ($menu->get_iterated() as $o) {
            $list = array();
            if (!in_array($o->id, $notin_id)) {
                $o->level = 1;
                $list[] = $o;
                $o->get_all_sub($list, 2, array('select' => $select));
                foreach ($list as $o1) {
                    if ($maxlevel == 0) {
                        $listname[$o1->id] = $o1->get_title_format();
                    } else {
                        if ($o1->level <= $maxlevel) {
                            $listname[$o1->id] = $o1->get_title_format();
                        }
                    }
                }
            }
        }
    }

    // --------------------------------------------------------------------
    /**
     * string of object's title in formatted.
     * 
     * @access public
     * @param string
     * @return strign
     */
    function get_title_format($space = '-') {
        if (!isset($this->level))
            $this->level = $this->level();
        $str = str_repeat($space, $this->level * 5);
        return $str . $this->title;
    }
    
    function total(){
    	$this->select("count(id) as total");
    	$this->get();
    	
		return array(
					"title" => $this->lang->line('menu'),
					"total"	=> $this->total,
				);	
    }

}

/* End of file menu.php */
/* Location: ./application/models/menu.php */